﻿using AutojsController.Helpers;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace AutojsController.Config
{
    public class DBContext : DbContext
    {
        public DbSet<SystemConfig> systemConfig { get; set; }
        public DbSet<Mobile> mobiles { get; set; }
        public DbSet<Script> scripts { get; set; }
        public DbSet<Group> groups { get; set; }

        public DBContext(DbContextOptions<DBContext> options) : base(options)
        {
        }
    }

    public class DBContextFactory : IDesignTimeDbContextFactory<DBContext>
    {
        public DBContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<DBContext>();

            optionsBuilder.UseSqlite("Data Source=config.db");

            return new DBContext(optionsBuilder.Options);
        }
    }

    public class SystemConfig
    {
        [Key]
        public string ConfigKey { get; set; } = null!;
        public string? ConfigValue { get; set; }
    }

    [Index(nameof(Address), IsUnique = true)]
    public class Mobile
    {
        public int Id { get; set; }
        public string Address { get; set; } = null!;
        public string? DeviceType { get; set; }
        public int? GroupId { get; set; }
        public Group? Group { get; set; }
    }

    [Index(nameof(Name), IsUnique = true)]
    public class Script
    {
        public int Id { get; set; }
        public string Path { get; set; } = null!;
        public string Name { get; set; } = null!;
        public ScriptType Type { get; set; } = ScriptType.Single;
        public bool Validated { get; set; } = false;
    }

    [Index(nameof(Name), IsUnique = true)]
    public class Group
    {
        public int Id { get; set; }
        public string Name { get; set; } = null!;
        public int? ScriptID { get; set; }
        public Script? Script { get; set; }
    }
}
    
